package com.wu.client.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.client.constant.FansTypeEnum;
import com.wu.client.constant.RedisKeyConstant;
import com.wu.client.constant.RelationShipTypeEnum;
import com.wu.client.service.ArtistService;
import com.wu.client.service.RelationshipService;
import com.wu.client.utils.PageRespUtil;
import com.wu.client.vo.req.artist.ArtistReq;
import com.wu.client.vo.res.PageResp;
import com.wu.client.vo.res.artist.ArtistBaseResp;
import com.wu.client.vo.res.artist.ArtistResp;
import com.wu.mysql.entiy.ArtistDO;
import com.wu.mysql.entiy.RelationshipDO;
import com.wu.mysql.mapper.ArtistMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class ArtistServiceImpl extends ServiceImpl<ArtistMapper, ArtistDO> implements ArtistService {

    private final StringRedisTemplate stringRedisTemplate;

    private final RelationshipService relationshipService;

    @Override
    public PageResp<ArtistBaseResp> getArtists(ArtistReq req) {
        Integer region = req.getRegion();
        Integer type = req.getType();
        Integer page = req.getPage();
        Integer limit = req.getLimit();
        Long userId = req.getUserId();
        Page<ArtistDO> artistDOPage = new Page<>(page, limit);
        artistDOPage = query()
                .eq(ObjectUtil.isNotNull(region), "region", region)
                .eq(ObjectUtil.isNotNull(type), "type", type)
                .page(artistDOPage);
        PageResp<ArtistBaseResp> artistBaseRespPageResp = PageRespUtil.buildPageResp(artistDOPage, ArtistBaseResp.class);
        List<ArtistBaseResp> items = artistBaseRespPageResp.getItems();

        if (ObjectUtil.isNotNull(userId)) {
            // 处理 pipeline 返回结果
            List<Object> results = stringRedisTemplate.executePipelined((RedisCallback<Object>) connection -> {
                for (ArtistBaseResp item : items) {
                    String redisKey = RedisKeyConstant.getFansRedisKey(FansTypeEnum.ARTIST.getCode(), item.getId());
                    connection.sIsMember(redisKey.getBytes(), userId.toString().getBytes());
                    connection.sCard(redisKey.getBytes());
                }
                return null;
            });
            for (int i = 0; i < items.size(); i++) {
                Boolean member = (Boolean) results.get(i * 2); // sIsMember 结果
                Long size = (Long) results.get(i * 2 + 1); // sCard 结果
                ArtistBaseResp item = items.get(i);
                item.setIsFollowed(member);
                item.setFollowerCount(size);
            }
        }
        return artistBaseRespPageResp;
    }



    @Override
    public ArtistResp getArtisById(Long id, Long userId) {
        ArtistDO artistDO = this.getById(id);
        ArtistResp artistResp = BeanUtil.toBean(artistDO, ArtistResp.class);
        if (ObjectUtil.isNotNull(userId)) {
            Boolean member = stringRedisTemplate.opsForSet().isMember(RedisKeyConstant.getFansRedisKey(FansTypeEnum.ARTIST.getCode(), id), userId.toString());
            artistResp.setIsFollowed(member);
        }
        return artistResp;
    }

}
